<template>
  <BaseLayout title="代码生成" @query="init" @reset="reset">
    <template #button>
      <el-button type="primary" @click="selectTable">选择表</el-button>
    </template>
    <template #query>
      <el-form
          :model="queryForm"
      >
        <el-form-item prop="templateName" label="表名称">
          <el-input type="text" v-model="queryForm.tableName" placeholder="请输入表名称"/>
        </el-form-item>
      </el-form>
    </template>
    <template #table>
      <el-table height="100%" v-loading="loading" :data="data">
        <el-table-column type="index" label="序号" width="60" align="center" header-align="center"/>
        <el-table-column prop="tableName" label="表名称"/>
        <el-table-column prop="className" label="类名称" align="center" header-align="center" width="180">
        </el-table-column>
        <el-table-column prop="descName" label="描述名称" align="center" header-align="center" width="180">
        </el-table-column>
        <el-table-column label="操作" width="300" align="center" header-align="center">
          <template #default="scope">
            <el-button link type="primary" @click="selectTemplate(scope.$index, scope.row,'download')">下载</el-button>
            <el-button link type="primary" @click="selectTemplate(scope.$index, scope.row,'create')">路径创建</el-button>
            <el-button link type="primary" @click="create(scope.$index, scope.row)">修改</el-button>
            <el-button link type="danger" @click="deleteTableInfo( scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <CreateCode  @load="init"  ref="createCodeRef"></CreateCode>
      <SelectTable @load="init" ref="selectTableRef"></SelectTable>
      <SelectTemplate @load="createCodeCallback" ref="selectTemplateRef"></SelectTemplate>
    </template>
    <template #page>
      <BasePage @load="init" v-model:current="queryForm.current" v-model:size="queryForm.size"
                :total="queryForm.total"></BasePage>
    </template>
  </BaseLayout>
</template>

<script setup lang="ts" name="codeCreate">
import {reactive, ref} from "vue";
import CreateCode from './CreateCode.vue'
import SelectTable from './SelectTable.vue'
import SelectTemplate from './SelectTemplate.vue'
import {
  createCode,
  deleteTableInfoByTableId,
  downloadCode,
  listTableInfoPage,
  selectTableInfoByTableId
} from "/@/api/generator/TableInfoApi";
import {TableInfoModel} from "/@/api/generator/model/TableInfoModel";
import {downloadFile} from "/@/utils/http/axios/HttpUtil";
import {messageBox} from "/@/utils/BjxUtils";
const createCodeRef = ref()
const selectTableRef = ref()
const selectTemplateRef = ref()
const queryForm = reactive({
  tableName: '',
  current: 1,
  size: 10,
  total: 0
})
const loading = ref(false)
const data = ref<TableInfoModel[]>([])
const init = async () => {
  loading.value = true
  let res = await listTableInfoPage(queryForm,()=>{
    loading.value = false
  });
  data.value = res.records
  queryForm.total = res.total
}
const reset = () => {
  queryForm.tableName = ''
  queryForm.current = 1
  init()
}
const selectTable = () => {
  selectTableRef.value.init()
}
const create = async (index: number,row: TableInfoModel) => {
  loading.value = true
  let res = await  selectTableInfoByTableId(row.tableId,()=>{
    loading.value = false
  });
  createCodeRef.value.init(res)
}
/**
 * 删除表信息
 */
const deleteTableInfo = (row: TableInfoModel) => {
  messageBox("您确定要删除这条表信息吗?",async ()=>{
    loading.value = true
    await deleteTableInfoByTableId(row.tableId)
    init()
  })
}

/**
 * 下载代码
 */
const downloadCodeFile = async (templateIds: string[],tableId: string) => {
  loading.value = true
  let res = await downloadCode({templateIds,tableId},()=>{
    loading.value = false
  })
  downloadFile(res)
}
/**
 * 路径创建
 */
const pathCreate = async(templateIds: string[],tableId: string) => {
  messageBox("路径创建会覆盖您已有的代码,您确定要创建代码吗?",async ()=>{
    loading.value = true
    await createCode({templateIds,tableId},()=>{
      loading.value = false
    })
  })
}
/**
 * 生成代码回调
 * @param templateIds
 * @param tableId
 * @param flag
 */
const createCodeCallback = (templateIds: string[],tableId: string,flag: string) =>{
     if("create" === flag){
       pathCreate(templateIds,tableId)
     }else{
       downloadCodeFile(templateIds,tableId)
     }
}
/**
 * 选择模板
 */
const selectTemplate = (index: number,row: TableInfoModel,flag: string) =>{
  selectTemplateRef.value.init(row.tableId,flag)
}
const remove = () =>{

}
</script>

<style scoped>

</style>